home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / findfile / findfile.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-09-06  |  6.2 KB  |  215 lines

  1. VERSION 2.00
  2. Begin Form FindFile 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Find File"
  5.    ClientHeight    =   2520
  6.    ClientLeft      =   1095
  7.    ClientTop       =   1590
  8.    ClientWidth     =   7365
  9.    Height          =   2925
  10.    Icon            =   FINDFILE.FRX:0000
  11.    Left            =   1035
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   2520
  14.    ScaleWidth      =   7365
  15.    Top             =   1245
  16.    Width           =   7485
  17.    Begin CommandButton CancelBtn 
  18.       Caption         =   "Cancel"
  19.       Height          =   510
  20.       Left            =   5850
  21.       TabIndex        =   5
  22.       Top             =   1710
  23.       Visible         =   0   'False
  24.       Width           =   1230
  25.    End
  26.    Begin CommandButton OKBtn 
  27.       Caption         =   "OK"
  28.       Height          =   510
  29.       Left            =   5850
  30.       TabIndex        =   4
  31.       Top             =   1125
  32.       Width           =   1230
  33.    End
  34.    Begin TextBox Containing 
  35.       Height          =   285
  36.       Left            =   4095
  37.       TabIndex        =   1
  38.       Top             =   675
  39.       Width           =   2985
  40.    End
  41.    Begin PictureBox IncludeSub 
  42.       Height          =   285
  43.       Left            =   2925
  44.       ScaleHeight     =   255
  45.       ScaleWidth      =   2190
  46.       TabIndex        =   2
  47.       Top             =   1260
  48.       Width           =   2220
  49.    End
  50.    Begin TextBox FileSpec 
  51.       Height          =   285
  52.       Left            =   4635
  53.       MaxLength       =   12
  54.       TabIndex        =   0
  55.       Text            =   "*.*"
  56.       Top             =   180
  57.       Width           =   2445
  58.    End
  59.    Begin DirListBox Dir1 
  60.       Height          =   2055
  61.       Left            =   315
  62.       TabIndex        =   6
  63.       Top             =   135
  64.       Width           =   2310
  65.    End
  66.    Begin DriveListBox Drive1 
  67.       Height          =   315
  68.       Left            =   3015
  69.       TabIndex        =   3
  70.       Top             =   1845
  71.       Width           =   2355
  72.    End
  73.    Begin Label Label1 
  74.       BackStyle       =   0  'Transparent
  75.       Caption         =   "Containing:"
  76.       Height          =   240
  77.       Index           =   1
  78.       Left            =   2970
  79.       TabIndex        =   8
  80.       Top             =   720
  81.       Width           =   1095
  82.    End
  83.    Begin Label Label1 
  84.       BackStyle       =   0  'Transparent
  85.       Caption         =   "File Specification:"
  86.       Height          =   240
  87.       Index           =   0
  88.       Left            =   2970
  89.       TabIndex        =   7
  90.       Top             =   225
  91.       Width           =   1770
  92.    End
  93. Option Explicit
  94. Option Compare Text
  95. Dim F1 As Found
  96. Dim CancelFlag As Integer
  97. Sub CancelBtn_Click ()
  98. CancelFlag = True
  99. End Sub
  100. Sub Drive1_Change ()
  101. Dir1.Path = Left$(Drive1.Drive, 2)
  102. End Sub
  103. Function FileContains (FileName As String, SearchText As String) As Integer
  104. Dim FileNumber As Integer
  105. Dim FileLength As Long
  106. Dim Chunk As String
  107. Dim ChunkStart As Long
  108. Const MaxChunk = 20000
  109. On Error GoTo FileContainsError
  110. FileNumber = FreeFile
  111. Open FileName For Binary Access Read Shared As FileNumber
  112. FileLength = LOF(FileNumber)
  113. ChunkStart = 0
  114. Do Until ChunkStart = FileLength
  115.     If FileLength - ChunkStart > MaxChunk Then
  116.         Chunk = Input$(MaxChunk, FileNumber)
  117.         ChunkStart = ChunkStart + MaxChunk - Len(SearchText)
  118.     Else
  119.         Chunk = Input$(FileLength - ChunkStart, FileNumber)
  120.         ChunkStart = FileLength
  121.     End If
  122.     If InStr(Chunk, SearchText) > 0 Then
  123.         FileContains = True
  124.         Exit Do
  125.     End If
  126. Close FileNumber
  127. Exit Function
  128. FileContainsError:
  129.     Select Case Err
  130.         Case Else
  131.             MsgBox Error$ & " on file " & FileName
  132.     End Select
  133.     Exit Function
  134. End Function
  135. Sub Find (SearchPath As String)
  136. ReDim DirName(0 To 15) As String
  137. Dim DirCount As Integer
  138. Dim FileName As String, Attributes As Integer
  139. Dim x As Integer
  140. If Right$(SearchPath, 1) <> "\" Then SearchPath = SearchPath & "\"
  141. DirCount = 0
  142. FileName = Dir$(SearchPath & FileSpec, Attr_Normal + Attr_System + Attr_Hidden)
  143. Do Until FileName = ""
  144.     If Containing = "" Then
  145.         F1.FoundFiles.AddItem SearchPath & FileName
  146.     Else
  147.         If FileContains(SearchPath & FileName, (Containing.Text)) Then
  148.             F1.FoundFiles.AddItem SearchPath & FileName
  149.         End If
  150.     End If
  151.     FileName = Dir$
  152.     DoEvents
  153.     If CancelFlag Then Exit Sub
  154. If IncludeSub Then
  155.     FileName = Dir$(SearchPath & "*.*", Attr_Normal + Attr_System + Attr_Hidden + Attr_Directory)
  156.     Do Until FileName = ""
  157.         If FileName <> "." And FileName <> ".." Then
  158.             Attributes = GetAttr(SearchPath & FileName)
  159.             If (Attributes And Attr_Directory) Then
  160.                 If DirCount > UBound(DirName) Then
  161.                     ReDim Preserve DirName(0 To DirCount + 15)
  162.                 End If
  163.                 DirName(DirCount) = SearchPath & FileName
  164.                 DirCount = DirCount + 1
  165.             End If
  166.         End If
  167.         FileName = Dir$
  168.         DoEvents
  169.         If CancelFlag Then Exit Sub
  170.     Loop
  171.     For x = 0 To DirCount - 1
  172.         Find DirName(x)
  173.     Next x
  174. End If
  175. End Sub
  176. Sub Form_Unload (Cancel As Integer)
  177. If Forms.Count > 1 Then
  178.     Select Case MsgBox("Close search windows also?", MB_YesNoCancel)
  179.         Case IDYes
  180.             End
  181.         Case IDCancel
  182.             Cancel = True
  183.     End Select
  184. End If
  185.             
  186. End Sub
  187. Sub OKBtn_Click ()
  188. 'MousePointer = Hourglass
  189. OKBtn.Enabled = False
  190. Caption = "Find File - Searching"
  191. CancelBtn.Visible = True
  192. Set F1 = New Found
  193. CancelFlag = False
  194. If FileSpec = "" Then FileSpec = "*.*"
  195. Find (Dir1.Path)
  196. Caption = "Find File"
  197. CancelBtn.Visible = False
  198. If CancelFlag Then
  199.     Unload F1
  200.     Select Case F1.FoundFiles.ListCount
  201.         Case 0
  202.             MsgBox "No files matching the search criteria were found."
  203.             Unload F1
  204.         Case 1
  205.             F1.Caption = F1.FoundFiles.ListCount & " File Found"
  206.             F1.Show
  207.         Case Else
  208.             F1.Caption = F1.FoundFiles.ListCount & " Files Found"
  209.             F1.Show
  210.     End Select
  211. End If
  212. OKBtn.Enabled = True
  213. 'MousePointer = Default
  214. End Sub
  215.